package net.hn.hnms.biz.safety.wind.Dao;

import net.hn.hnms.biz.safety.wind.entity.AirVolumeEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @author 智呈物联 nzw
 * @date 2022/12/22
 */
public interface AirVolumeDao extends JpaRepository<AirVolumeEntity,Long> {

    @Query(value = "SELECT\n" +
            "\t* \n" +
            "FROM\n" +
            "\tair_volume \n" +
            "WHERE\n" +
            "CASE\n" +
            "\t\t\n" +
            "\t\tWHEN ?1!= '' THEN\n" +
            "\tTRIM ( mine_code ) = ?1 ELSE 1 = 1 \n" +
            "END",nativeQuery = true)
    List<AirVolumeEntity> findAllByMineCode(String mineCode);

    @Query(value = "SELECT\n" +
            "cast(position_id as VARCHAR),cast(\"max\"(data_time) as VARCHAR) " +
            "FROM\n" +
            "\tair_volume \n" +
            "WHERE\n" +
            "CASE\n" +
            "\t\t\n" +
            "\t\tWHEN ?1!= '' THEN\n" +
            "\tTRIM ( mine_code ) = ?1 ELSE 1 = 1 end \n" +
            "GROUP BY\n" +
            "\tposition_id",nativeQuery = true)
    List<Map<String,String>> findByPositionId(String mineCode);

    @Query(value = "SELECT\n" +
            "\t* \n" +
            "FROM\n" +
            "\tair_volume \n" +
            "WHERE\n" +
            "\tposition_id = ?1 \n" +
            "\tAND data_time = ?2 \n" +
            "\tLIMIT 1 OFFSET 0",nativeQuery = true)
    AirVolumeEntity findByMaxDataTime(Long positionId, Timestamp dataTime);
}
